﻿using DataBaseBackupTools.FmChild;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using ModelLibrary;

namespace DataBaseBackupTools
{
    public partial class FmTask : Form
    {
        public FmTask()
        {
            InitializeComponent();
            this.TopLevel = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int dgvrIndex = e.RowIndex;
            FmInstanceChild FIC = new FmInstanceChild(dgvrIndex);
            if (FIC.ShowDialog() == DialogResult.OK)
            {
                RefreshList();
                dataGridView1.CurrentCell = dataGridView1[0, dgvrIndex];
            }
        }
        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ViewImage();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            添加ToolStripMenuItem_Click(null, new EventArgs());
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }

        private void btnStartAll_Click(object sender, EventArgs e)
        {

        }

        private void btnStopAll_Click(object sender, EventArgs e)
        {

        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FmTaskChild ftc = new FmTaskChild();
            ftc.ShowDialog();
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 启用ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 停用ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void RefreshList()//刷新界面
        {
            if (!System.IO.File.Exists(AppSets.InstancePath))
            {
                return;
            }
            List<Instance> liInst = XmlHelper.XmlDeserializeFromFile<LiInstances>(AppSets.InstancePath, AppSets.Encodings);
            if (liInst.Count == 0 || liInst == null)
            {
                return;
            }
            this.dataGridView1.Columns["sort"].DataPropertyName = "sort";
            this.dataGridView1.Columns["taskName"].DataPropertyName = "taskName";
            this.dataGridView1.Columns["databaseType"].DataPropertyName = "databaseType";
            this.dataGridView1.Columns["linkInfo"].DataPropertyName = "linkInfo";
            this.dataGridView1.Columns["modifyTime"].DataPropertyName = "modifyTime";
            this.dataGridView1.Columns["status"].DataPropertyName = "taskStatus";
            this.dataGridView1.DataSource = liInst;

            //ViewImage();

        }

        private void ViewImage()//刷新图片显示
        {

            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if (dgvr.Cells["status"].Value == null)
                {
                    dgvr.Cells["taskStatus"].Value = dgvr.Cells["status"].Value;
                }
                else
                    dgvr.Cells["taskStatus"].Value = (bool)dgvr.Cells["status"].Value ? AppSets.imgTrue : AppSets.imgFalse;
            }

        }
    }
}
